using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Reflection;
using QTool.Reflection;
using UnityEngine.Networking;
using System.Threading.Tasks;
using QTool.NetFrame;
using QTool.BEPUphysics.Entities;

namespace QTool.NetFrame
{
    [RequireComponent(typeof(QNetFrameEntity))]
    public abstract class QFixBehaviour : MonoBehaviour
    {
        
        [HideInInspector]
        [UnityEngine.Serialization.FormerlySerializedAs("Id")]
        [SerializeField]
        private QNetFrameEntity netEntity;
        public QNetFrameEntity NetEntity => netEntity;
        public InputActions Input
        {
            get
            {
                return QNetManager.Instance.Inputs[netEntity.playerId];
            }
        }
        public FixVector3 Position { get => netEntity.Position; set { netEntity.Position = value; } }
        public FixQuaternion Rotation { get => netEntity.Rotation; set { netEntity.Rotation = value; } }
        public GameObject FixInstantiate(GameObject prefab, FixVector3 position, FixQuaternion rotation) 
        {
            return QNetManager.FixInstantiate(prefab, position, rotation,(netId)=>netId.playerId=NetEntity.playerId);
        }
        public void FixDestroy(QNetFrameEntity netObj)
        {
            QNetManager.FixDestroy(netObj);
        }
        public IEnumerator StartFixCoroutine(IEnumerator enumerator)
        {
            return netEntity.FixCoroutine.StartCoroutine(enumerator);
        }
        public void StopFixCoroutine(IEnumerator enumerator)
        {
            netEntity.FixCoroutine?.StopCoroutine(enumerator);
        }
        /// <summary>
        /// 主机对象
        /// </summary>
        public bool IsHost => QNetManager.Instance.IsHost && QNetManager.Instance.PlayerId == netEntity.playerId;
        /// <summary>
        /// 本地玩家对象
        /// </summary>
        public bool IsLocalPlayer => QNetManager.Instance.PlayerId == netEntity.playerId;
        public string PlayerName => QNetManager.Instance.PlayerList[netEntity.playerId].playerName;
        public static Fix64 DeltaTime
        {
            get
            {
                return QNetManager.Instance.DelatTime;
            }
        }
        private void Reset()
        {
            netEntity = GetComponent<QNetFrameEntity>();
        }
        private void OnValidate()
        {
            if (netEntity == null)
            {
                netEntity = GetComponent<QNetFrameEntity>();
            }
        }
        public bool FixActive => netEntity.FixActive;
        public virtual void OnFixAwake()
        {
          
        }
		public virtual void OnFixStart()
		{

		}
        public virtual void OnFixUpdate()
        {

        }
        public virtual void OnFixDestroy()
        {
           
        }
      
    }
}
